#!/usr/bin/python
# coding=utf8
#
# Copyright (C) 2011 XNData contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


__author__ = "zhangchitc@gmail.com"


import xndata.data.note
import xndata.client
import re
import urllib2
from BeautifulSoup import BeautifulSoup
import time, datetime

class NoteUrlMgr:

    @staticmethod
    def others_note_list_url (author_id, cur_page):
        return "http://blog.renren.com/blog/%s/friends?curpage=%d" % (
                author_id, cur_page)

    @staticmethod
    def my_note_list_url (cur_page):
        return "http://blog.renren.com/blog/0?__view=async-html&curpage=%d" % cur_page

    @staticmethod
    def note_url (author_id, note_id):
        return "http://blog.renren.com/blog/%s/%s" % (author_id, note_id)


class NotePageFetcher:

    @staticmethod
    def fetch_others_note_list_page (opener, author_id, cur_page):
        request = urllib2.Request (NoteUrlMgr.others_note_list_url (author_id, cur_page))
        return opener.open (request).read ()

    @staticmethod
    def fetch_my_note_list_page (opener, cur_page):
        request = urllib2.Request (NoteUrlMgr.my_note_list_url (cur_page))
        return opener.open (request).read ()

    @staticmethod
    def fetch_note_page (opener, author_id, note_id):
        request = urllib2.Request (NoteUrlMgr.note_url (author_id, note_id))
        return opener.open (request).read ()


def nvgstr2str (s):
    return unicode (s).encode ('utf-8')


class NotePageParser:
    
    @staticmethod
    def get_total_note (html):
        # get rid of embedded javascript
        # They will have bad influence on BeautifulSoup html parsing
        while html.find ("<script") != -1:
            a = html.find ("<script")
            b = html.find ("/script>")
            html = html[:a] + html[b + 8:]
        soup = BeautifulSoup (html)
        
        node = soup.find ("div", { "class" : "blog-home" }).find (
                            "div", { "class" : "pager-top" } )
        total_str = node.span.string
        total_str = total_str[total_str.find ("/") + 2:]
        total_str = total_str[:-1]
        return int (total_str)

    @staticmethod
    def get_note_list (html, author_id, cur_page):
        # get rid of embedded javascript
        # They will have bad influence on BeautifulSoup html parsing
        while html.find ("<script") != -1:
            a = html.find ("<script")
            b = html.find ("/script>")
            html = html[:a] + html[b + 8:]
        soup = BeautifulSoup (html)
        
        nodelist = soup.find ("div", { "class" : "blog-home" }).findAll (
                                "div", { "class" : "list-blog" } )
        note_list = xndata.data.note.NoteList (
                    author_id = author_id, cur_page = cur_page)
        for node in nodelist:
            href = str (node.strong.a["href"])
            m = re.compile (r"/blog/\d*/(?P<noteid>\d*)").search (href)
            note_id = m.group ("noteid")
            
            title = nvgstr2str (node.strong.a.string)
            
            time_str = str (node.find ("span", { "class" : "timestamp" }).string).strip ()
            time_struct = time.strptime (time_str, "%Y-%m-%d %H:%M")
            pub_time = datetime.datetime (*time_struct[0:5])

            note = xndata.data.note.SimpleNote (
                    note_id = note_id, author_id = author_id, title = title, 
                    pub_time = pub_time)
            note_list.addNote (note)
        return note_list

    @staticmethod
    def get_note (html, author_id, note_id):
        # get rid of embedded javascript
        # They will have bad influence on BeautifulSoup html parsing
        while html.find ("<script") != -1:
            a = html.find ("<script")
            b = html.find ("/script>")
            html = html[:a] + html[b + 8:]
        soup = BeautifulSoup (html)
        
        node = soup.find ("div", { "class" : "blog-home" })

        title = nvgstr2str (node.strong.string)
        
        time_str = str (node.find ("span", { "class" : "timestamp" }).contents[0].strip ())
        time_struct = time.strptime (time_str, "%Y-%m-%d %H:%M")
        pub_time = datetime.datetime (*time_struct[0:5])

        content = str (node.find ("div", { "id" : "blogContent" }))
        
        note = xndata.data.note.Note (author_id = author_id, note_id = note_id, 
                    title = title, pub_time = pub_time, content = content)
        return note



if __name__ == "__main__":
    client = xndata.client.XNClient ("zhangchitc@gmail.com", "aaaaaaaa")
    #html = NotePageFetcher.fetch_others_note_list_page (client.opener, "247780420", 0)
    #NotePageParser.get_note_list (html, 0,0 )
    #html = NotePageFetcher.fetch_my_note_list_page (client.opener, 0)
    html = NotePageFetcher.fetch_note_page (client.opener, "242124580", "737669617")
    NotePageParser.get_note (html, 0, 0)
